package com.liuguang.framework.utils;

import android.content.Context;
import android.content.res.AssetManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class AssetCopyer {
    private AssetManager mAssetManager ;
    private final Context mContext;

    public AssetCopyer(Context context) {
        this.mContext = context;
        mAssetManager = this.mContext.getAssets();
    }

    public String getAssetsProperty(String str) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.mAssetManager.open(new File(str).getPath())));
        StringBuilder sb = new StringBuilder();
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine == null) {
                return sb.toString();
            }
            sb.append(readLine);
        }
    }

    public File copyFile(String str, File file) throws IOException {
        InputStream open = this.mAssetManager.open(new File(str).getPath());
        if (-1 != str.indexOf("/")) {
            str = str.substring(str.indexOf("/"));
        }
        File file2 = new File(file, str);
        file2.getParentFile().mkdirs();
        FileOutputStream fileOutputStream = new FileOutputStream(file2);
        byte[] bArr = new byte[1024];
        while (true) {
            int read = open.read(bArr);
            if (read == -1) {
                break;
            } else if (read == 0) {
                int read2 = open.read();
                if (read2 < 0) {
                    break;
                }
                fileOutputStream.write(read2);
            } else {
                fileOutputStream.write(bArr, 0, read);
            }
        }
        fileOutputStream.close();
        return file2;
    }
}
